package cn.rui.listen;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.rui.pojo.LoginLog;
import cn.rui.pojo.User;
import cn.rui.pojo.vo.CityInfo;
import cn.rui.service.LoginLogService;
import cn.rui.service.UserService;
import cn.rui.utils.IpUtils;
import cn.rui.utils.QueryCityInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 自定义侦听器的实现
 *
 * @author 徽州大都督
 * @date 2021/05/13
 */
@Component
public class MySaTokenListener implements SaTokenListener {


    @Autowired
    private LoginLogService loginLogService;

    @Autowired
    private UserService userService;


    /**
     * 每次登录时触发
     */
    @Override
    public void doLogin(String s, Object loginName, SaLoginModel saLoginModel) {

        LoginLog loginLog = MySaTokenListener.setLoginLog ();

        loginLog.setGetDate (new Date ());
        loginLog.setContent ("登录成功");

        User user = userService.findUserNameByUUid (loginName.toString ());

        loginLog.setLoginName (user.getUsername ());
        loginLogService.save (loginLog);
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String s, Object loginName, String s1) {

        //删除数据库token
        userService.updateToken (loginName.toString ());

        LoginLog loginLog = MySaTokenListener.setLoginLog ();
        loginLog.setGetDate (new Date ());
        loginLog.setContent ("注销成功");


        User user = userService.findUserNameByUUid (loginName.toString ());

        loginLog.setLoginName (user.getUsername ());
        loginLogService.save (loginLog);
    }


    /**
     * 每次被踢下线时触发
     */
    @Override
    public void doLogoutByLoginId(String s, Object loginName, String s1, String s2) {

        //删除数据库token
        userService.updateToken (loginName.toString ());

        LoginLog loginLog = MySaTokenListener.setLoginLog ();
        loginLog.setGetDate (new Date ());
        loginLog.setContent ("被踢下线");

        User user = userService.findUserNameByUUid (loginName.toString ());

        loginLog.setLoginName (user.getUsername ());
        loginLogService.save (loginLog);

    }

    /**
     * 每次被顶下线时触发
     */
    @Override
    public void doReplaced(String s, Object loginName, String s1, String s2) {
//        LoginLog loginLog = new LoginLog ();
//        loginLog.setGetDate (new Date ());
//        loginLog.setContent ("被顶下线");
//        User user=userService.findUserNameByUUid(loginName.toString ());
//
//        loginLog.setLoginName (user.getUsername ());
//        loginLogService.save(loginLog);
    }

    /**
     * 每次被封禁时触发
     */
    @Override
    public void doDisable(String s, Object loginName, long l) {
        LoginLog loginLog = MySaTokenListener.setLoginLog ();
        loginLog.setGetDate (new Date ());
        loginLog.setContent ("被封禁");

        User user = userService.findUserNameByUUid (loginName.toString ());

        loginLog.setLoginName (user.getUsername ());
        loginLogService.save (loginLog);
    }

    /**
     * 每次被解封时触发
     */
    @Override
    public void doUntieDisable(String s, Object loginName) {
        LoginLog loginLog = MySaTokenListener.setLoginLog ();
        loginLog.setGetDate (new Date ());
        loginLog.setContent ("被解封");

        User user = userService.findUserNameByUUid (loginName.toString ());

        loginLog.setLoginName (user.getUsername ());
        loginLogService.save (loginLog);
    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCreateSession(String s) {

    }

    /**
     * 每次注销Session时触发
     */
    @Override
    public void doLogoutSession(String s) {

    }


    public static LoginLog setLoginLog() {
        LoginLog loginLog = new LoginLog ();

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes ();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;

        HttpServletRequest request = servletRequestAttributes.getRequest ();
        String ipAdrress = IpUtils.getIpAdrress (request);
        CityInfo cityInfo = QueryCityInfoUtils.queryCity (ipAdrress);
        if (cityInfo.getStatus () != 0) {

            loginLog.setPostalCode (10001);
            loginLog.setIpAddress ("0.0.0.0");
            loginLog.setCityLocation ("中国");
            return loginLog;
        }

        loginLog.setCityLocation (cityInfo.getResult ().getAd_info ().getNation () +
                " " + cityInfo.getResult ().getAd_info ().getProvince () +
                " " + cityInfo.getResult ().getAd_info ().getCity () +
                " " + cityInfo.getResult ().getAd_info ().getDistrict ()
        );
        loginLog.setIpAddress (ipAdrress);

        loginLog.setPostalCode (cityInfo.getResult ().getAd_info ().getAdcode ());

        return loginLog;
    }
}
